package com.lrkj.waitress.controller;

import com.lrkj.common.annotation.Log;
import com.lrkj.common.constant.UserConstants;
import com.lrkj.common.core.controller.BaseController;
import com.lrkj.common.core.domain.AjaxResult;
import com.lrkj.common.core.page.TableDataInfo;
import com.lrkj.common.enums.BusinessType;
import com.lrkj.common.utils.poi.ExcelUtil;
import com.lrkj.waitress.domain.CustomInfo;
import com.lrkj.waitress.domain.RoomCustom;
import com.lrkj.waitress.service.ICustomInfoService;
import com.lrkj.waitress.service.IRoomCustomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 客户管理Controller
 *
 * @author yajun_ren
 * @date 2022-03-17
 */
@RestController
@RequestMapping("/waitress/customInfo")
@Api(tags = "客户管理")
public class CustomInfoController extends BaseController {
    @Autowired
    private ICustomInfoService customInfoService;

    @Autowired
    private IRoomCustomService roomCustomService;

    /**
     * 查询客户管理列表
     */
    @ApiOperation(value = "查询客户管理列表", notes = "查询客户管理列表")
    @PreAuthorize("@ss.hasPermi('waitress:customInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomInfo customInfo) {
        startPage();
        this.changeParameter(customInfo.getParams());
        List<CustomInfo> list = customInfoService.selectCustomInfoList(customInfo);
        return getDataTable(list);
    }

    /**
     * 导出客户管理列表
     */
    @ApiOperation(value = "导出客户管理列表", notes = "导出客户管理列表")
    @PreAuthorize("@ss.hasPermi('waitress:customInfo:export')")
    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CustomInfo customInfo) {
        List<CustomInfo> list = customInfoService.selectCustomInfoList(customInfo);
        ExcelUtil<CustomInfo> util = new ExcelUtil<CustomInfo>(CustomInfo.class);
        return util.exportExcel(list, "客户管理数据");
    }

    /**
     * 获取客户管理详细信息
     */
    @ApiOperation(value = "获取客户管理详细信息", notes = "获取客户管理详细信息")
    @PreAuthorize("@ss.hasPermi('waitress:customInfo:query')")
    @GetMapping(value = "/{customId}")
    public AjaxResult getInfo(@PathVariable("customId") String customId) {
        return AjaxResult.success(customInfoService.selectCustomInfoById(customId));
    }

    /**
     * 新增客户管理
     */
    @ApiOperation(value = "新增客户管理", notes = "新增客户管理")
    @PreAuthorize("@ss.hasPermi('waitress:customInfo:add')")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomInfo customInfo) {
        if (UserConstants.NOT_UNIQUE.equals(customInfoService.checkCodeUnique(customInfo))) {
            return AjaxResult.error("客户名称'" + customInfo.getCustomName() + "'已存在");
        }
        return toAjax(customInfoService.insertCustomInfo(customInfo));
    }

    /**
     * 修改客户管理
     */
    @ApiOperation(value = "修改客户管理", notes = "修改客户管理")
    @PreAuthorize("@ss.hasPermi('waitress:customInfo:edit')")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomInfo customInfo) {
        if (UserConstants.NOT_UNIQUE.equals(customInfoService.checkCodeUnique(customInfo))) {
            return AjaxResult.error("客户名称'" + customInfo.getCustomName() + "'已存在");
        }
        return toAjax(customInfoService.updateCustomInfo(customInfo));
    }

    /**
     * 删除客户管理
     */
    @ApiOperation(value = "删除客户管理", notes = "删除客户管理")
    @PreAuthorize("@ss.hasPermi('waitress:customInfo:remove')")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{customIds}")
    public AjaxResult remove(@PathVariable String[] customIds) {
        return toAjax(customInfoService.deleteCustomInfoByIds(customIds));
    }


    /**
     * 业主成员列表
     */
    @ApiOperation(value = "业主成员列表", notes = "业主成员列表")
    @GetMapping(value = "/roomCustomList")
    public AjaxResult roomCustomList(RoomCustom roomCustom) {
        this.changeParameter(roomCustom.getParams());
        return AjaxResult.success(roomCustomService.selectRoomCustomList(roomCustom));
    }


    /**
     * 新增业主成员
     */
    @ApiOperation(value = "新增业主成员", notes = "新增业主成员")
    @Log(title = "业主成员", businessType = BusinessType.INSERT)
    @PostMapping("/addFamily")
    public AjaxResult addFamily(@RequestBody RoomCustom roomCustom) {
        return toAjax(roomCustomService.doSavaOrUpdate(roomCustom));
    }

    @Log(title = "客户信息导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, String currencyCommunityId) throws Exception
    {
        ExcelUtil<CustomInfo> util = new ExcelUtil<CustomInfo>(CustomInfo.class);
        List<CustomInfo> beanList = util.importExcel(file.getInputStream());
        String message = customInfoService.importData(beanList, currencyCommunityId);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<CustomInfo> util = new ExcelUtil<CustomInfo>(CustomInfo.class);
        return util.importTemplateExcel("客户信息模板");
    }

}
